#include "app_state_feedback.h"

#include "app_communication.h"
#include "app_flank.h"
#include "app_hip.h"
#include "app_lumbar.h"
#include "app_massage.h"
#include "app_thigh.h"
#include "app_vent_heat.h"
#include "common_util.h"

/**
 * @brief 腰托状态映射表
 *
 */
typedef struct
{
    lumbar_u_d_manual_state_e u_d_state;
    lumbar_f_r_manual_state_e f_r_state;
} lumbar_state_mapping_t;

static void app_state_feedback_lumbar_support_state_handler(void);
static void app_state_feedback_massage_state_handler(void);
static void app_state_feedback_flank_state_handler(void);
static void app_state_feedback_thigh_state_handler(void);
static void app_state_feedback_hip_state_handler(void);
static void app_state_feedback_general_handler(void);
static void app_state_feedback_vent_heat_state_handler(void);

static lumbar_state_mapping_t lumbar_mode_to_state_map[] = {
    [LUMBAR_MODE_FRONT]    = { LUMBAR_U_D_STATE_KEEP, LUMBAR_F_R_STATE_MOVE_FRONT },
    [LUMBAR_MODE_REAR]     = { LUMBAR_U_D_STATE_KEEP, LUMBAR_F_R_STATE_MOVE_REAR },
    [LUMBAR_MODE_F_R_KEEP] = { LUMBAR_U_D_STATE_KEEP, LUMBAR_F_R_STATE_KEEP },
    [LUMBAR_MODE_UP]       = { LUMBAR_U_D_STATE_MOVE_UP, LUMBAR_F_R_STATE_KEEP },
    [LUMBAR_MODE_DOWN]     = { LUMBAR_U_D_STATE_MOVE_DOWN, LUMBAR_F_R_STATE_KEEP },
    [LUMBAR_MODE_U_D_KEEP] = { LUMBAR_U_D_STATE_KEEP, LUMBAR_F_R_STATE_KEEP },
};

/**
 * @brief 状态反馈处理函数，在调度器中循环执行
 *
 */
void app_state_feedback_handle(void)
{
    /* 腰托状态处理 */
    app_state_feedback_lumbar_support_state_handler();

    /* 按摩状态处理 */
    app_state_feedback_massage_state_handler();

    /* 侧翼系统状态处理 */
    app_state_feedback_flank_state_handler();

    /* 腿托状态处理 */
    app_state_feedback_thigh_state_handler();

    /* 臀部状态处理 */
    app_state_feedback_hip_state_handler();

    /* 通用状态处理 */
    app_state_feedback_general_handler();

    /* 通风加热状态处理 */
    app_state_feedback_vent_heat_state_handler();
}

/**
 * @brief 通风加热状态处理函数
 *
 */
static void app_state_feedback_vent_heat_state_handler(void)
{
    /* 通风加热模块状态 */
    hvs_fl_sts1.can_data_u.signal.dr_heater_cooler_sw_feedback_pneumatic = app_vent_heat_get_mode();
}

/**
 * @brief 通用状态处理函数
 *
 */
static void app_state_feedback_general_handler(void)
{
    /* 外部大气压值 */
    uint32_t ext_air_pressure = cps121_sensor_list[CPS121_EXYERNAL_IDX].air_pressure;
    hvs_fl_sts5.can_data_u.signal.dr_external_air_pressure_value =
        common_util_air_pressure_to_signal(ext_air_pressure);
}

/**
 * @brief 臀部状态处理函数
 *
 */
static void app_state_feedback_hip_state_handler(void)
{
    /* 臀部气袋支撑状态 */
    hvs_fl_sts1.can_data_u.signal.dr_highsupport_ctrl_state_up_pneumatic =
        app_hip_get_airbag_state(HIP_AIRBAG_IDX_UP);
    hvs_fl_sts1.can_data_u.signal.dr_highsupport_ctrl_state_down_pneumatic =
        app_hip_get_airbag_state(HIP_AIRBAG_IDX_DOWN);

    /* 臀部手动调节状态  */
    hip_mode_e mode = app_hip_get_mode();
    switch (mode) {
    case GENERAL_MODE_KEEP:
        hvs_fl_sts1.can_data_u.signal.dr_highsupport_manual_state_up_pneumatic   = HIP_UP_STATE_KEEP;
        hvs_fl_sts1.can_data_u.signal.dr_highsupport_manual_state_down_pneumatic = HIP_DOWN_STATE_KEEP;
        break;
    case HIP_MODE_FRONT:
        hvs_fl_sts1.can_data_u.signal.dr_highsupport_manual_state_up_pneumatic = HIP_UP_STATE_FRONT;
        break;
    case HIP_MODE_REAR:
        hvs_fl_sts1.can_data_u.signal.dr_highsupport_manual_state_up_pneumatic = HIP_UP_STATE_REAR;
        break;
    case HIP_MODE_UP_KEEP:
        hvs_fl_sts1.can_data_u.signal.dr_highsupport_manual_state_up_pneumatic = HIP_UP_STATE_KEEP;
        break;
    case HIP_MODE_UP:
        hvs_fl_sts1.can_data_u.signal.dr_highsupport_manual_state_down_pneumatic = HIP_DOWN_STATE_UP;
        break;
    case HIP_MODE_DOWN:
        hvs_fl_sts1.can_data_u.signal.dr_highsupport_manual_state_down_pneumatic = HIP_DOWN_STATE_DOWN;
        break;
    case HIP_MODE_DOWN_KEEP:
        hvs_fl_sts1.can_data_u.signal.dr_highsupport_manual_state_down_pneumatic = HIP_DOWN_STATE_KEEP;
        break;
    }

    /* 臀部气袋气压值 */
    uint32_t hip_up_air_pressure   = app_hip_get_air_pressure(HIP_AIRBAG_IDX_UP);
    uint32_t hip_down_air_pressure = app_hip_get_air_pressure(HIP_AIRBAG_IDX_DOWN);
    hvs_fl_sts5.can_data_u.signal.dr_hip_up_support_air_pressure_value =
        common_util_air_pressure_to_signal(hip_up_air_pressure);
    hvs_fl_sts5.can_data_u.signal.dr_hip_down_support_air_pressure_value =
        common_util_air_pressure_to_signal(hip_down_air_pressure);
}

/**
 * @brief 腿托状态处理函数
 *
 */
static void app_state_feedback_thigh_state_handler(void)
{
    hvs_fl_sts1.can_data_u.signal.dr_thighsupport_ctrl_state_pneumatic =
        app_thigh_get_airbag_state(THIGH_AIRBAG_IDX_0);
    hvs_fl_sts1.can_data_u.signal.dr_thighsupport_manual_state_pneumatic = app_thigh_get_mode();

    /* 获取腿托气袋气压值 */
    uint32_t thigh_airbag_air_pressure = app_thigh_get_air_pressure(THIGH_AIRBAG_IDX_0);
    hvs_fl_sts3.can_data_u.signal.dr_thigh_support_air_pressure_valve =
        common_util_air_pressure_to_signal(thigh_airbag_air_pressure);
}

/**
 * @brief 侧翼系统状态处理函数
 *
 */
static void app_state_feedback_flank_state_handler(void)
{
    hvs_fl_sts1.can_data_u.signal.dr_bolstersupport_ctrl_state_pneumatic =
        app_flank_get_airbag_state(FLANK_AIRBAG_IDX_LEFT_RIGHT_BIG);
    hvs_fl_sts1.can_data_u.signal.dr_bolstersupport_manual_state_pneumatic = app_flank_get_mode();

    /* 获取侧翼大气袋气压值 */
    uint32_t flank_big_airbag_air_pressure = app_flank_get_air_pressure(FLANK_AIRBAG_IDX_LEFT_RIGHT_BIG);
    hvs_fl_sts3.can_data_u.signal.dr_boloster_support_air_pressure_valve =
        common_util_air_pressure_to_signal(flank_big_airbag_air_pressure);
}
/**
 * @brief 按摩状态处理函数
 *
 */
static void app_state_feedback_massage_state_handler(void)
{
    hvs_fl_sts2.can_data_u.signal.dr_massage_mode_state_pneumatic = app_massage_get_current_mode();
    hvs_fl_sts2.can_data_u.signal.dr_massage_intensity_state_pneumatic =
        app_massage_get_current_intensity();
    hvs_fl_sts2.can_data_u.signal.dr_massage_time_state_pneumatic   = app_massage_get_time();
    hvs_fl_sts2.can_data_u.signal.dr_massage_switch_state_pneumatic = app_massage_get_switch();
}

/**
 * @brief 腰托状态处理函数
 *
 */
static void app_state_feedback_lumbar_support_state_handler(void)
{
    /* 获取上下气袋支撑状态 */
    hvs_fl_sts1.can_data_u.signal.dr_lumbar_ctrl_state_up_pneumatic =
        app_lumbar_get_airbag_state(LUMBAR_AIRBAG_IDX_UP);
    hvs_fl_sts1.can_data_u.signal.dr_lumbar_ctrl_state_down_pneumatic =
        app_lumbar_get_airbag_state(LUMBAR_AIRBAG_IDX_DOWN);

    /* 获取腰托手动调节状态（腰托工作模式）*/
    lumbar_mode_e mode = app_lumbar_get_mode();

    /* 通过查表设置状态 */
    hvs_fl_sts1.can_data_u.signal.dr_lumbar_u_d_manual_state_pneumatic =
        lumbar_mode_to_state_map[mode].u_d_state;
    hvs_fl_sts1.can_data_u.signal.dr_lumbar_f_r_manual_state_pneumatic =
        lumbar_mode_to_state_map[mode].f_r_state;

    /* 获取腰托气袋气压值 */
    uint32_t up_air_pressure   = app_lumbar_get_air_pressure(LUMBAR_AIRBAG_IDX_UP);
    uint32_t down_air_pressure = app_lumbar_get_air_pressure(LUMBAR_AIRBAG_IDX_DOWN);

    hvs_fl_sts3.can_data_u.signal.dr_lumbar_up_support_air_pressure_valve =
        common_util_air_pressure_to_signal(up_air_pressure);
    hvs_fl_sts3.can_data_u.signal.dr_lumbar_down_support_air_pressure_valve =
        common_util_air_pressure_to_signal(down_air_pressure);
}
